//
//  ViewController.m
//  26-GCD
//
//  Created by GivenBmli on 16/8/18.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    1.什么是GCD (Grand(伟大的) Central(中央) Dispatch(调度))
//    2.GCD的核心概念 队列,任务拆分分配到对列中,系统自动为我们创建线程和调整线程 自动
//    3.GCD中的对列严格遵循FIFO(先进先出)原则
//          对列分类
//            1.串行 DISPATCH_QUEUE_SERIAL /NULL
//            2.并发 DISPATCH_QUEUE_CONCURRENT
//    4.对列的创建
//      1.>队列名字
//      2.>队列类型
//    串行队列
//    自己创建
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
//     并发队列
    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
//    系统创建
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
//    全局对列
//    参数1: 调度对列优先级
//    参数2: 给未来使用,占位使用 填0;
    
   dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    术语:
//    串行,一个任务执行完毕,在执行下一个
//    并发,多个任务同时执行
    
//   5.同步  异步
//    同步 在当前线程执行任务,不能开辟线程
//    异步 在新的线程执行任务,可以开辟线程
//    同步串行对列 不会创建线程
//    [self syncSerialQueue:serialQueue];
//    同步 并发队列 不会创建线程
//      [self syncConcurretQueue:concurrentQueue];
//    同步 主线程队列
//    [self syncmainQueue:mainQueue];
//    同步 全局队列 不会创建线程
//    [self syncglobalQueue:globalQueue];
//    2>异步
//    异步串行队列 创建一条新的线程在该线程中执行任务
//    [self asyncSerialQueue:serialQueue];
//    异步并发队列 根据任务决定需要创建多少线程
//    [self asyncConcurretQueue:concurrentQueue];
//    异步 主线程队列
//    [self asyncmainQueue:mainQueue];
//    异步 全局队列
//    [self asyncglobalQueue:globalQueue];
    
//    6._group组
//    1>创建组
    dispatch_group_t grop = dispatch_group_create();
    dispatch_queue_t concurrentQueue1 = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
//    2>组中只有异步
//    添加多个任务
    dispatch_group_async(grop, concurrentQueue1, ^{
        NSLog(@"group1 = %@",[NSThread currentThread]);
    });
    dispatch_group_async(grop, concurrentQueue1, ^{
        NSLog(@"group2 = %@",[NSThread currentThread]);
    });
    dispatch_group_async(grop, concurrentQueue1, ^{
        NSLog(@"group3 = %@",[NSThread currentThread]);
    });
    dispatch_group_async(grop, concurrentQueue1, ^{
        NSLog(@"group4 = %@",[NSThread currentThread]);
    });
    dispatch_group_async(grop, concurrentQueue1, ^{
        NSLog(@"group5 = %@",[NSThread currentThread]);
    });
//    所有任务都执行完毕,会掉用下列方法
    dispatch_group_notify(grop, mainQueue, ^{
        NSLog(@"所有完成后是,执行后续刷新");
    });
    
}

- (void)syncSerialQueue:(dispatch_queue_t)serialQueue {
//    block 中的内容会追加到队列中
    NSLog(@"同步串行对列");
    for (int i = 0; i< 10; i++) {
        dispatch_sync(serialQueue, ^{
            
            NSLog(@"%d----%@",i,[NSThread currentThread]);
            
            
        });

    }

    
    
}

- (void)syncConcurretQueue:(dispatch_queue_t)concurrentQueue {
    NSLog(@"同步并发对列");
    for (int i = 0; i< 10; i++) {
        dispatch_sync(concurrentQueue, ^{
            
                NSLog(@"%d --- %@",i,[NSThread currentThread]);
            
        });
    }

}

- (void)syncmainQueue:(dispatch_queue_t)mainQueue {
    NSLog(@"同步主线程队列");
    //    主线程 同步 主线程
    //    主线程等待同步操作结束, 同步操作在主线程执行,互相等待,导致程序卡死
    for (int i = 0; i< 10; i++) {
        dispatch_sync(mainQueue, ^{
            NSLog(@"ddd%d --- %@",i,[NSThread currentThread]);
        });
    }
}

- (void)syncglobalQueue:(dispatch_queue_t)globalQueue {
    for (int i = 0; i< 10; i++) {
        NSLog(@"同步全局队列");
        dispatch_sync(globalQueue, ^{
            NSLog(@"ddd%d --- %@",i,[NSThread currentThread]);
        });
    }

    
}
/*********************************/
- (void)asyncSerialQueue:(dispatch_queue_t)serialQueue {
    NSLog(@"异步串行对列");
    for (int i = 0; i< 10; i++) {
        dispatch_async(serialQueue, ^{
            NSLog(@"%d ----%@",i,[NSThread currentThread]);
        });
    }
}
- (void)asyncConcurretQueue:(dispatch_queue_t)concurrentQueue {
    NSLog(@"异步并发对列");
    for (int i = 0; i< 10; i++) {
        dispatch_async(concurrentQueue, ^{
            NSLog(@"%d ----%@",i,[NSThread currentThread]);
        });
    }
    
}
- (void)asyncmainQueue:(dispatch_queue_t)mainQueue {
     NSLog(@"异步主线程对列");
    for (int i = 0; i< 10; i++) {
        dispatch_async(mainQueue, ^{
            NSLog(@"%d ----%@",i,[NSThread currentThread]);
        });
    }
}
- (void)asyncglobalQueue:(dispatch_queue_t)globalQueue {
    NSLog(@"异步全局对列");
    for (int i = 0; i< 10; i++) {
        dispatch_async(globalQueue, ^{
            NSLog(@"%d ----%@",i,[NSThread currentThread]);
        });

    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
